package com.twu.biblioteca.ui;

import com.twu.biblioteca.IRender;
import com.twu.biblioteca.modal.Message;

import java.util.Scanner;

public class Shell {
    private final Menu menu;
    private final Scanner scanner;
    private final IRender render;

    public Shell(Menu menu, Scanner scanner, IRender render) {
        this.menu = menu;
        this.scanner = scanner;
        this.render = render;
    }

    private void renderNewLine() {
        render.render("\n");
    }

    public void run() {
        render.render(getMessage());
        renderNewLine();
        showMenuOptions();
        while (scanner.hasNextLine()) {
            String command = scanner.nextLine();
            if (command.trim().length() > 0) {
                deal(command);
                showMenuOptions();
            }
        }
    }

    private void showMenuOptions() {
        render.render("=====================\n");
        render.render(Message.SELECT_MENU_OPTIONS);
        render.renderNewLine();
        render.render(menu.show());
        render.renderNewLine();
        render.render(">");
    }

    public void deal(String command) {
        if (menu.verify(command)) {
            menu.input(command);
        } else {
            render.render(Message.VAILD_MENU_OPTIONS);
            render.renderNewLine();
        }
    }

    public String getMessage() {
        return Message.WELCOME;
    }
}
